package com.test.base;


import java.lang.reflect.ParameterizedType;







import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.test.domain.User;
import com.test.service.DepartmentService;
import com.test.service.ForumService;
import com.test.service.PriviledgeService;
import com.test.service.ReplyService;
import com.test.service.RoleService;
import com.test.service.TopicService;
import com.test.service.UserService;
/**
 * 定义了一个基础的Action，采用了模型驱动方式封装参数
 * @author SingleHe 2015-10-8
 *
 * @param <T>
 */
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
	//===========================声明Service==============================
	//使用了注解方式从容器中获得对应的service
//	@Resource
//	protected JuesInfoService juesInfoService;
//	@Resource
//	protected UserInfoService userInfoService;
	//=========================对ModelDriven的支持=========================
	private static final long serialVersionUID = 1L;
	//准备公共的service 子类继承时自动获得，对应的service
	protected UserService userService;
	protected RoleService roleService;
	protected DepartmentService departmentService;
	protected PriviledgeService priviledgeService;
	protected ForumService forumService;
	protected TopicService topicService;
	protected ReplyService replyService;
	protected T model;
	@SuppressWarnings("unchecked")
	public BaseAction(){
		System.out.println("----------> BaseAction.BaseAction()");
		try{
			//通过反射获得T的真实类型
			ParameterizedType pt = (ParameterizedType)this.getClass().getGenericSuperclass();
			Class<T> clazz = (Class<T>)pt.getActualTypeArguments()[0];
			//通过反射创建Model的实例
			model = clazz.newInstance();
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public T getModel() {
		// TODO Auto-generated method stub
		return model;
	}

	public PriviledgeService getPriviledgeService() {
		return priviledgeService;
	}

	public void setPriviledgeService(PriviledgeService priviledgeService) {
		this.priviledgeService = priviledgeService;
	}

	public ForumService getForumService() {
		return forumService;
	}

	public void setForumService(ForumService forumService) {
		this.forumService = forumService;
	}

	public TopicService getTopicService() {
		return topicService;
	}

	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}

	public ReplyService getReplyService() {
		return replyService;
	}

	public void setReplyService(ReplyService replyService) {
		this.replyService = replyService;
	}
	/**
	 * 获得当前的登录用户
	 * @return
	 */
	public User getCurrentUser(){
		return (User)ActionContext.getContext().getSession().get("user");
	}
	/**
	 * 获得当前请求IP
	 * @return
	 */
	public String getRequestIp(){
		return ServletActionContext.getRequest().getRemoteAddr();
	}
}
